﻿using System.Collections.Generic;
using System;
using VoxelBlock.VoxelBlock3D;
using VoxelBlock;using Vector3Int = VoxelBlock.Vector3Int;

public class OperationImplementRectangleSelect : OperationImplement
{

    List<Vector3Int> oldSerializeVoxel = new List<Vector3Int>();
    //Dictionary<int, Vector3Int>  allSelectVoxel;
    Vector3Int rectangleOldPosition;
    VoxelCategories.OperationSelectOperator selectOperator;
    //DrawVoxel3DCore nowDrawVoxel3DCore;
    Vector3Int[] zeroVector3Int = new Vector3Int[0];


    //public OperationImplementRectangleSelect(ProxyOperation proxy) : base(proxy)
    //{
        
    //}

    public override void Start(StartArg arg)
    {
        this.selectOperator = arg.selectOperator;
        base.Start(arg);
        //this.allSelectVoxel = nowDrawVoxel3DCore.allSelectVoxel;
    }

    public override void Run(RunArg arg)
    {
        if (rectangleOldPosition != arg.nowSelectPosition)
        {
            rectangleOldPosition = arg.nowSelectPosition;

            var arrayPoint = BasicAlgorithm.GetRectanglePointArray(arg.gestureStartPosition, rectangleOldPosition);

            switch (selectOperator)
            {
                case VoxelCategories.OperationSelectOperator.Union:
                    nowDrawVoxel3DCore.DrawSelect(oldSerializeVoxel, 255);
                    oldSerializeVoxel = nowDrawVoxel3DCore.DrawSelect(arrayPoint, 1);
                    break;
                case VoxelCategories.OperationSelectOperator.Remove:
                    nowDrawVoxel3DCore.DrawSelect(oldSerializeVoxel, 1);
                    oldSerializeVoxel = nowDrawVoxel3DCore.DrawSelect(arrayPoint, 255);
                    break;
                default:
                    break;
            }
        }
    }

    public override void Over(OverArg arg)
    {
        if (oldSerializeVoxel.Count > 0)
        {
            switch (selectOperator)
            {
                case VoxelCategories.OperationSelectOperator.Union:
                    //foreach (var item in oldSerializeVoxel)
                    //{
                    //    allSelectVoxel[item.GetHashCode()] = item;
                    //}
                    proxy.Write(VoxelCategories.Operation.SelectAdd, zeroVector3Int, oldSerializeVoxel);
                    oldSerializeVoxel = new List<Vector3Int>();
                    break;
                case VoxelCategories.OperationSelectOperator.Remove:
                    //Dictionary<Vector3Int, Vector3Int> oldAllSelectVoxel = new Dictionary<Vector3Int, Vector3Int>(allSelectVoxel.Count);
                    //foreach (var item in allSelectVoxel)
                    //{
                    //    oldAllSelectVoxel[item] = item;
                    //}
                    //foreach (var item in oldSerializeVoxel)
                    //{
                    //    oldAllSelectVoxel.Remove(item);
                    //}
                    //allSelectVoxel.Clear();
                    //allSelectVoxel.AddRange(oldAllSelectVoxel.Keys);
                    //oldAllSelectVoxel.Clear();
                    //foreach (var item in oldSerializeVoxel)
                    //{
                    //    allSelectVoxel.Remove(item.GetHashCode());
                    //}

                    proxy.Write(VoxelCategories.Operation.SelectRemove, oldSerializeVoxel, zeroVector3Int);
                    oldSerializeVoxel = new List<Vector3Int>();
                    break;
                default:
                    break;
            }
        }
    }

}


